﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _81文件流复制文件
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            CopyFIle(@"C:\Users\UnityPC\Desktop\Feishu-3.37.4.exe", @"D:\");
        }

        /// <summary>
        /// 把file复制到dir中
        /// </summary>
        /// <param name="file">原文件</param>
        /// <param name="dir">目标文件夹</param>
        private static void CopyFIle(string file, string dir)
        {
            //获得目标文件路径
            string fileName = Path.GetFileName(file);
            string targetFile = Path.Combine(dir, fileName);

            using (FileStream fsRead = File.OpenRead(file), fsWrite = File.Create(targetFile))
            {
                //定义一个1M缓冲区
                byte[] array = new byte[1024 * 1024];

                //重复读写
                while (true)
                {
                    int length = fsRead.Read(array, 0, array.Length);//都取到缓冲区
                    if (length == 0)
                    {
                        break;
                    }
                    fsWrite.Write(array, 0, length);
                    Console.WriteLine("进度:{0}", (fsRead.Position / (float)fsRead.Length).ToString("p"));
                }
            }
        }
    }
}